<!-- 问诊贴管理 -->
<template>
  <ContainerSlot>
    <template slot="categoryBar">
      <div class="search-bar">
        <div class="btn-list">
          <div class="arrow-left" />
          <!-- 问诊贴 -->
          <div v-for="item,index in categoryList" :key="item.id" class="btn-item" v-clickDown='index'
            :class="{ active: invitationTypeId == item.id }" @click="searchBar(item.id)">{{ item.name }}</div>
          <div class="arrow-right" />
        </div>
      </div>
    </template>
    <template slot="searchModule">
      <SearchForm :label-width="labelWidth" :form-config="formConfig" :form-data="formData" :slot-list="slotList">
        <template slot="searchBtn">
          <el-button type="primary" @click.prevent="submitForm()">查询</el-button>
        </template>
      </SearchForm>
    </template>
    <template slot="contentModule">
      <div v-show="postList.length>0" class="container">
        <!-- item.invitationId -->
        <div v-for="(item,index) in postList" :key="index" class="containerItem">
          <div class="left">
            <div class="title">{{ item.invitationTitle }}</div>
            <div class="personInfo">
              <div class="name">{{ item.publisher.nickName }}</div>
              <div class="time">{{ item.createTime }}</div>
            </div>
          </div>
          <div class="right">
            <span :style="item.choicenessFlag?'color: red;': ''" @click="goRefining(item)">加精</span>
            <span @click="goTop(item)">置顶</span>
            <span @click="del(item)">删除</span>
            <!-- <span>用户体验</span> -->
          </div>
          <img v-show="item.topFlag" src="@/assets/specialist-consultation/inquiry-management/speech-management/zhiding-2.png" class="img">
        </div>
      </div>
      <div v-show="postList.length==0" class="nodata">
        暂无数据~
      </div>
    </template>
    <!-- 分页器 -->
    <template slot="footerModule">
      <el-pagination :current-page="currentPage" background :page-size="pageSize" :page-sizes="[10, 20, 30]" layout=" prev, pager, next, jumper" :total="total" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
    </template>
  </ContainerSlot>
</template>

<script>
import ContainerSlot from "@/components/Container";
import SearchForm from "@/components/Form";
import CategoryBar from "@/components/CategoryBar";
import speechManagementApi from "@/api/specialist-consultation/inquiry-management/speech-management/index";

export default {
  name: "",
  components: { ContainerSlot, SearchForm, CategoryBar },
  data() {
    return {
      total: 1,
      currentPage: 1,
      pageSize: 10,
      labelWidth: "100px",
      // 搜索框
      formConfig: [
        {
          label: "关键词",
          prop: "name1",
          type: "input",
          width: "150px",
        },
      ],
      // 查询按钮或重置
      slotList: [
        {
          slotName: "searchBtn",
        },
      ],
      // 表单数据
      formData: {
        name1: "",
      },
      // 导航栏
      categoryList: [],
      routerIndex: 0,
      // 帖子列表
      postList: [],
      invitationTypeId: "",
    };
  },
  created() {
    // 获取帖子分类
    this.getClassificationList();
    // 获取问诊贴子
    this.getPostList();
  },
  directives: {
   clickDown: {
     inserted(el,binding,index) {
       if(binding.value===0){
         el.click()
       }
     }
   }
 },
  methods: {
    searchBar(e) {
      this.invitationTypeId = e;
      this.getPostList();
      console.log('this.invitationTypeId**********',this.invitationTypeId);
    },
    // 获取分类帖子
    async getClassificationList() {
      const res = await speechManagementApi.queryClassification({ name: "" });
      console.log('获取导航栏问诊贴分类',res);
      if (res.code === 200) {
        this.categoryList = res.data;
        this.invitationTypeId = this.categoryList[0].invitationTypeId;
        this.categoryList.forEach((item) => {
          item.id = item.invitationTypeId;
        });
      }
    },
    // 各类问诊贴子 - 条件分页查询
    async getPostList(pages = 1, keywords = "") {
      if (pages === 0) {
        pages = 1;
      }
      const params = {
        page: pages,
        size: this.pageSize,
        condition: {
          invitationTypeId: this.invitationTypeId,
          invitationTitle: keywords,
        },
      };
      const res = await speechManagementApi.queryPageByCondition(params);
      if (res.code === 200) {
        this.postList = res.data.content;
        this.total = res.data.totalElements;
      }
    },
    // 页数
    handleCurrentChange(e) {
      this.currentPage = e;
      this.getPostList(e, this.formData.name1);
    },
    handleSizeChange(e) {
      this.pageSize = e;
      this.getPostList();
    },
    // 切换导航栏
    categoryBar(item) {
      // 初始化
      this.formData.name1 = "";
      this.invitationTypeId = item.invitationTypeId;
      this.currentPage = 1;
      console.log(item);
      this.getPostList();
    },
    // 查询
    submitForm() {
      this.currentPage = 1;
      this.getPostList(1, this.formData.name1);
    },
    // 加精
    async goRefining(item) {
      const params = {
        invitationId: item.invitationId,
        choicenessFlag: true,
      };
      if (item.choicenessFlag) {
        params.choicenessFlag = false;
        const res = await speechManagementApi.updatePost(params);
        if (res.code === 200) {
          this.$message({
            message: "取消加精",
            type: "success",
          });
          this.getPostList(this.currentPage, this.formData.name1);
        }
      } else {
        params.choicenessFlag = true;
        const res = await speechManagementApi.updatePost(params);
        if (res.code === 200) {
          this.$message({
            message: "加精成功",
            type: "success",
          });
          this.currentPage = 1;
          this.getPostList(1, this.formData.name1);
        }
      }
    },
    // 置顶
    async goTop(item) {
      const params = {
        invitationId: item.invitationId,
        topFlag: true,
      };
      if (item.topFlag) {
        params.topFlag = false;
        const res = await speechManagementApi.updatePost(params);
        if (res.code === 200) {
          this.$message({
            message: "取消置顶",
            type: "success",
          });
          this.getPostList(this.currentPage, this.formData.name1);
        }
      } else {
        params.topFlag = true;
        const res = await speechManagementApi.updatePost(params);
        if (res.code === 200) {
          this.$message({
            message: "置顶成功",
            type: "success",
          });
          this.currentPage = 1;
          this.getPostList(1, this.formData.name1);
        }
      }
    },
    // 删除帖子
    del(item) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const params = {
            id: item.invitationId,
          };
          const res = await speechManagementApi.delPost(
            params,
            item.invitationId
          );
          if (res.code === 200) {
            this.$message({
              message: "删除成功",
              type: "success",
            });
            this.getPostList(
              this.postList.length > 1 ? this.currentPage : this.currentPage - 1
            );
            console.log(
              this.postList.length > 1 ? this.currentPage : this.currentPage - 1
            );
          } else {
            this.$message({
              message: "删除失败",
              type: "error",
            });
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>

<style lang="scss" scoped>
::v-deep .content-module {
  background: #f8f8f8 !important;
}
::v-deep .childModule {
  overflow: auto !important;
}
.container {
  width: 100%;
  height: 100%;
  background: #f8f8f8;
  .containerItem {
    position: relative;
    display: flex;
    justify-content: space-between;
    height: 82px;
    margin-bottom: 9px;
    padding: 17px;
    padding-left: 22px;
    background: #fff;
    border-radius: 5px;
    box-shadow: 0px 0px 3px 1px rgba(0, 0, 0, 0.16);
    .left {
      flex: 1;
      .tilte {
        font-size: 18px;
        font-weight: 500;
        color: #404040;
      }
      .personInfo {
        display: flex;
        margin-top: 10px;
        font-size: 16px;
        color: #989898;
        .time {
          margin-left: 64px;
        }
      }
    }
    .right {
      display: flex;
      justify-content: space-around;
      align-items: center;
      width: 267px;
      height: 100%;
      font-size: 14px;
      font-weight: 500;
      span {
        cursor: pointer;
      }
      :nth-child(1) {
        color: #0480fb;
        border-bottom: 1px solid #0480fb;
      }
      :nth-child(2) {
        color: #22a50b;
        border-bottom: 1px solid #22a50b;
      }
      :nth-child(3) {
        color: #ff6565;
        border-bottom: 1px solid #ff6565;
      }
      :nth-child(4) {
        color: #ff7600;
        border-bottom: 1px solid #ff7600;
      }
    }
    .img {
      position: absolute;
      left: 0;
      top: 0;
    }
  }
}
.nodata {
  position: relative;
  width: 100%;
  // height: 100%;
  text-align: center;
  top: 50%;
}
@import "~@/styles/variables.scss";

.search-bar {
  width: 100%;
  height: 50px;
  // background-color: #f8f8f8;
  display: flex;
  align-items: center;
  // margin: 10px 0;

  .btn-list {
    position: relative;
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;

    .btn-item {
      width: 145px;
      height: 80%;
      margin-right: 5px;
      font-size: 16px;
      border: 1px solid #59a1e8;
      background-color: transparent;
      color: #59a1e8;
      text-align: center;
      line-height: 40px;
      cursor: pointer;

      &:hover {
        background-color: #329eef;
        color: #fff;
      }

      &.active {
        background-color: #329eef;
        color: #fff;
      }
    }

    .arrow-left {
      position: relative;
      width: 20px;
      height: 80%;
      background-color: rgba(4, 128, 251, 0.3);
      margin-right: 5px;

      &::before {
        content: "";
        position: absolute;
        top: 5px;
        left: -10px;
        width: 0px;
        height: 0px;
        border: 15px solid transparent;
        border-right: 10px solid #0480fb;
      }
    }

    .arrow-right {
      position: absolute;
      right: 0;
      width: 20px;
      height: 80%;
      background-color: rgba(4, 128, 251, 0.3);

      &::before {
        content: "";
        position: absolute;
        top: 5px;
        left: 5px;
        width: 0px;
        height: 0px;
        border: 15px solid transparent;
        border-left: 10px solid #0480fb;
      }
    }
  }
}
</style>
